<?php

function echo_xhtml_backtrace($full_trace)
{
  for ($i = 0; $i < count($full_trace); $i++)
  {
    $trace = $full_trace[$i];

    echo "<li>";

    if ($trace['class'])
      echo $trace['class'], $trace['type'];

    echo $trace['function'];

    echo "(<a onclick='show_backtrace_function_args($i)' class='show_function_args' href='#'>show args</a>)";

    $error_file_url = "/pressless/source" . str_replace(realpath(PRESSLESS_ROOT_DIR), '', $trace['file']);

    printf(', in <a href="%2$s">%1$s</a> on line <a href="%2$s#line-%3$s">%3$s</a>', $trace['file'], $error_file_url, $trace['line']);

    if ( !is_null($trace['args']) )
    {
      if ( !is_array($trace['args']) ) $trace['args'] = array($trace['args']);

      printf('<ol id="backtrace-function-%s-args" style="display: none;" class="args">', $i);
      for ($i = 0; $i < count($trace['args']); $i++) {
        $arg = $trace['args'][$i];

        echo '<li class="arg">';
        var_dump($arg);
        echo '</li>';
      }
      echo '</ol>';
    }

    echo "</li>";
  }

}

?>

<html>
  <head>
    <title></title>

    <style type="text/css">
    </style>
  </head>

  <body>
    <h1></h1>

    <p>
      <?php printf('%5$s: %1$s in <a href="%3$s">%2$s</a> on line <a href="%3$s#line-%4$s">%4$s</a>', $error_message, $error_file_name, $error_file_url, $error_line_num, $error_type) ?>
    </p>

    <h2>Call stack</h2>

    <ol>
      <?php echo_xhtml_backtrace($backtrace) ?>
    </ol> 

    <script type="text/javascript">
function show_backtrace_function_args(index)
{
  argsUlEl = document.getElementById('backtrace-function-' + index + '-args');

  argsUlEl.style.display = 'block';
}
    </script>
  </body>
</html>
